import threading  

maxSubThreadNumber = 3  


def task():  
    thName = threading.currentThread().name  
    print("start and wait run thread : %s" % thName)  
    eventLock.wait()  # 暂停运行，等待绿灯  
    print("green light, %s carry on run" % thName)  
    print("red light, %s stop run" % thName)  
    eventLock.wait()  # 暂停运行，等待绿灯  
    print("green light, %s carry on run" % thName)  
    print("sub thread %s run end" % thName)  


if __name__ == "__main__":  

    eventLock = threading.Event()  

    for i in range(maxSubThreadNumber):  
        subThreadIns = threading.Thread(target=task)  
        subThreadIns.start()  

    eventLock.set()  # 设置为绿灯  
    eventLock.clear()  # 设置为红灯  
    eventLock.set()  # 设置为绿灯 